home *** CD-ROM | disk | FTP | other *** search
- Path: isonews.bbn.hp.com!hpbblb!news
- From: Matthias Dittrich <matti>
- Newsgroups: comp.lang.c++
- Subject: Re: Output to file vs. cout
- Date: 11 Mar 1996 13:09:36 GMT
- Organization: Hewlett-Packard Co.
- Message-ID: <4i18mg$ba5@hpbblb.bbn.hp.com>
- References: <JL.96Mar7165152@thyme.id.dth.dk>
- NNTP-Posting-Host: trabant.bbn.hp.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=iso-8859-1
- Content-Transfer-Encoding: 8bit
- X-Mailer: Mozilla 1.1N (X11; I; HP-UX A.09.07 9000/712)
- X-URL: news:JL.96Mar7165152@thyme.id.dth.dk
-
- jl@id.dth.dk (J°rn Lind-Nielsen) wrote:
- >
- >Here's another of the probs that comes from porting C to C++:
- >
- >- I want to be able to redirect program output to either a file or cout.
- > Typically this would be selected by a commandline option ("-o outfile").
- >
- >- In C I would do something like this:
- >
- >
- > main()
- > {
- > FILE *outputfile;
- >
- > if (commandline == do_output_to_file)
- > outputfile = stdout;
- > else
- > outputfile = fopen(passed_filename, "w");
- >
- > fprintf(outfile, "Hello world\n");
- >
- > fclose(outfile); /* Maybe - not allways necassary */
- > }
- >
- >- The question is: How is this done in C++ ?
- >
- >.......
- ofstream outfile(passed_filename); /* this constructor opens your file */
- outfile << "Hello world" << endl; /* writes the string */
-
- The destructor closes the file and of course you should do some error checking.
-
- Good luck,
- Matthias
-
-